package com.lswms.modules.lswms.service.impl.receipt;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.ConvertUtils;
import com.lswms.modules.lswms.Constant.ReceiptConstant;
import com.lswms.modules.lswms.dao.base.SkuDao;
import com.lswms.modules.lswms.dao.receipt.ReceiptDao;
import com.lswms.modules.lswms.dao.receipt.ReceiptdetailDao;
import com.lswms.modules.lswms.dto.receipt.ReceiptdetailDTO;
import com.lswms.modules.lswms.entity.base.SkuEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetailEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetaillistEntity;
import com.lswms.modules.lswms.service.receipt.ReceiptdetailService;
import com.lswms.modules.lswms.service.receipt.ReceiptdetaillistService;

/**
 * 收货明细
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-13
 */
@Service
public class ReceiptdetailServiceImpl extends CrudServiceImpl<ReceiptdetailDao, ReceiptdetailEntity, ReceiptdetailDTO>
		implements ReceiptdetailService {

	@Autowired
	ReceiptDao receiptDao;
	@Autowired
	ReceiptdetaillistService  receiptdetaillistService;
	
	@Autowired
	SkuDao skuDao;

	@Override
	public QueryWrapper<ReceiptdetailEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String receiptkey = (String) params.get("receiptkey");
		String sku = (String) params.get("sku");
		String line = (String) params.get("line");
		String descr = (String) params.get("descr");

		QueryWrapper<ReceiptdetailEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "id", id);
		wrapper.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
		wrapper.eq(StringUtils.isNotBlank(sku), "lswms_receiptdetail.sku", sku);
		wrapper.eq(StringUtils.isNotBlank(line), "lswms_receiptdetail.line", line);
		wrapper.like(StringUtils.isNotBlank(descr), "descr", descr);

		wrapper.orderByDesc("receiptkey");
		wrapper.orderByAsc("line");

		return wrapper;
	}

	@Override
	public PageData<ReceiptdetailEntity> pageBySql(Map<String, Object> params) {
		IPage<ReceiptdetailEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, ReceiptdetailEntity.class);
	}

	@Override
	public Integer getReceiptdetailCount(String receiptkey) {
		Integer count = 0;
		if (StringUtils.isNotBlank(receiptkey)) {
			QueryWrapper<ReceiptdetailEntity> wrapper = new QueryWrapper<>();
			wrapper.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
			count = baseDao.selectCount(wrapper);
		}

		return count;
	}

	@Override
	public Integer getReceiptdetailLine(String receiptkey) {
		Integer count = 0;
		if (StringUtils.isNotBlank(receiptkey)) {
			// QueryWrapper<ReceiptdetailEntity> wrapper = new QueryWrapper<>();
			// wrapper.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
			// count = baseDao.selectCount(wrapper) ;
			count = baseDao.getReceiptdetailMaxLine(receiptkey);
		}

		return count;
	}

	@Override
	public ReceiptdetailEntity getBySql(Long id) {
		ReceiptdetailEntity entity = baseDao.selectByIdBySql(id);

		return ConvertUtils.sourceToTarget(entity, ReceiptdetailEntity.class);
	}

	@Override
	public String validSave(ReceiptdetailDTO dto) {
		// 如果收货单是关闭状态，则不允许新增
		QueryWrapper<ReceiptEntity> receiptQuery = new QueryWrapper<ReceiptEntity>();
		receiptQuery.eq("receiptkey", dto.getReceiptkey());

		ReceiptEntity receiptEntity = receiptDao.selectOne(receiptQuery);
		if (receiptEntity.getStatus().equals(ReceiptConstant.RECEIPTSTATUS_11)) {
			return " 收货单已关闭,不能操作";
		}

		// 验证商品资料是否存在
		String sku = dto.getSku();
		QueryWrapper<SkuEntity> skuQuery = new QueryWrapper<SkuEntity>();
		skuQuery.eq("sku", sku);
		Integer skuCount = skuDao.selectCount(skuQuery);
		if (skuCount <= 0) {
			return "物料信息不存在";
		}

		BigDecimal qty = dto.getQty();
		if (qty.compareTo(BigDecimal.ZERO) < 0) {
			return " 预计数量不能为负数";
		}
		BigDecimal  actqty = dto.getActqty();
		if (actqty.compareTo(BigDecimal.ZERO) < 0) {
			return " 数量不能为负数";
		}

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String validAndSave(ReceiptdetailDTO dto) {
		String msg = validSave(dto);
		if(StringUtils.isNotBlank(msg)) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return msg ;
		}
		
		Integer line = getReceiptdetailLine(dto.getReceiptkey());

		dto.setLine(line + 1);
		
		BigDecimal  actqty = dto.getActqty();
		
		dto.setActqty(BigDecimal.ZERO);
		save(dto);
		// 如果填写的实际收货量大于0，增加收货记录
		if(actqty.compareTo(BigDecimal.ZERO)>0) {
			ReceiptdetaillistEntity receiptdetaillistEntity = new ReceiptdetaillistEntity();
			receiptdetaillistEntity.setReceiptkey( dto.getReceiptkey() );
			receiptdetaillistEntity.setLine(dto.getLine());	
			receiptdetaillistEntity.setSku(dto.getSku());
			receiptdetaillistEntity.setUnit( dto.getUnit());
			receiptdetaillistEntity.setPack(dto.getPack());
			receiptdetaillistEntity.setQty(dto.getQty());
			receiptdetaillistEntity.setActqty(actqty);
			receiptdetaillistEntity.setToloc(dto.getToloc());
			receiptdetaillistEntity.setTotray(dto.getTotray());
			receiptdetaillistEntity.setTobox(dto.getTobox());
			receiptdetaillistEntity.setLot1(dto.getLot1());
			receiptdetaillistEntity.setLot2(dto.getLot2());
			receiptdetaillistEntity.setLot3(dto.getLot3());
			receiptdetaillistEntity.setLot4(dto.getLot4());
			receiptdetaillistEntity.setLot5(dto.getLot5());
			receiptdetaillistEntity.setLot6(dto.getLot6());
			receiptdetaillistEntity.setLot7(dto.getLot7());
			receiptdetaillistEntity.setLot8(dto.getLot8());
			receiptdetaillistEntity.setLot9(dto.getLot9());
			receiptdetaillistEntity.setLot10(dto.getLot10());
			receiptdetaillistEntity.setLot11(dto.getLot11());
			receiptdetaillistEntity.setLot12(dto.getLot12());
			receiptdetaillistEntity.setLot13(dto.getLot13());
			receiptdetaillistEntity.setLot14(dto.getLot14());
			receiptdetaillistEntity.setLot15(dto.getLot15());
			receiptdetaillistEntity.setSourcekey(dto.getSourcekey());
			receiptdetaillistEntity.setSourcetype(dto.getSourcetype());
			receiptdetaillistEntity.setSourcedate(dto.getSourcedate());
			receiptdetaillistEntity.setRelatedorderkey(dto.getRelatedorderkey());
			receiptdetaillistEntity.setRelatedorderline(dto.getRelatedorderline());
			receiptdetaillistEntity.setUniquecode(dto.getUniquecode());
			
			msg = receiptdetaillistService.receipt(receiptdetaillistEntity) ;
			if(StringUtils.isNotBlank(msg)) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return msg ;
			}
			
		}
		return null;
	}

}
